<template>
  <div>
    <a-card class="card-header">
      <a-form-model :model="queryParam">
        <a-row :gutter="24">
          <a-col :span="5">
            <a-form-model-item
              label="备料单号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-model.trim="queryParam.pickListNum"
                placeholder="请输入备料单号"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="5">
            <a-form-model-item
              label="加工单号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-model.trim="queryParam.worksheetNum"
                placeholder="请输入加工单号"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="2">
            <a-button
              type="primary"
              style="margin-top:3px"
              @click="handleSearch"
              >查询</a-button
            >
          </a-col>
        </a-row>
      </a-form-model>
    </a-card>

    <a-card class="card-content">
      <a-row :gutter="24">
        <a-col :span="22">
          <!-- <a-button type="primary" @click="handleAdd">新建</a-button> -->
          <!-- <a-button type="primary" @click="handleAdd">导出</a-button> -->
          <!-- <a-button type="primary" @click="handleAdd">导入</a-button> -->
        </a-col>
        <a-col :span="2">
          <a-button type="primary" @click="handleAllList('PickingList')"
            >全部备料单</a-button
          >
        </a-col>
      </a-row>
      <br />
      <a-table
        :columns="columns"
        :data-source="tableData"
        :loading="loading"
        :pagination="pagination"
        @change="handleTableChange"
        bordered
        :rowKey="(record, index) => record.id"
      >
        <template slot="wareHouseName" slot-scope="text, record">
          {{ getWareHouseName(record) }}
        </template>

        <template slot="action" slot-scope="text, record">
          <a @click="handleWare(record)">出库</a>
          <a-divider type="vertical" />
          <a @click="handleDetail(record)">详情</a>
          <a-divider type="vertical" />
          <a-icon
            type="qrcode"
            @click.stop="handleCode(record)"
            style="fontSize: 16px;color: #40a9ff;"
          />
        </template>
      </a-table>
    </a-card>
    <!-- 二维码 -->
    <qrCode
      ref="qrCode"
      :label="qrModel.label"
      :text="qrModel.text"
      :qrSize="320"
    ></qrCode>
  </div>
</template>

<script>
import { columns } from "./components/tableConfig";
import { tableMinxins } from "@/unit/mixins/tableMinxins.js";
import api from "@/api/apiServer";
import qrCode from "@/components/qrCode";
import { CodeNo } from "@/unit/QRCodeConfig";

export default {
  components: { qrCode },
  mixins: [tableMinxins],
  data() {
    return {
      columns,
      tableData: [],
      wareHouseList: [], // 仓库列表
      goodsList: [], // 商品列表
      qrModel: {
        label: "", // 二维码底部内容
        text: "", // 二维码内容
      },
    };
  },
  activated() {
    this.getList();
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 生成二维码
    handleCode(record) {
      let { pickListNum } = record;
      let newObj = { pickListNum, QRCode: CodeNo.Material };
      let text = JSON.stringify(newObj);
      this.$refs.qrCode.title = "备料二维码";
      this.qrModel.label = `${pickListNum}`;
      this.qrModel.text = text;
      this.$refs.qrCode.list = this.qrModel.label.split(",");
      this.$refs.qrCode.showModal();
    },
    // 获取备料单列表数据
    async getList(arg) {
      this.loading = true;
      if (arg === 1) {
        this.pagination.current = 1;
      }
      var params = this.getQueryParams();
      const res = await api.PickingList(params);
      this.tableData = res.data.data;
      this.pagination.total = res.data.total;
      this.loading = false;
    },
    // 获取仓库名称
    getWareHouseName(record) {
      for (var item of this.wareHouseList) {
        if (item.wareHouseNumber == record.wareHouseNum) {
          return item.wareHouseName;
        }
      }
    },
    // 跳转到出库单页面
    handleWare(record) {
      console.log(record.worksheetId);
      this.$router.push({
        path: "/picking/wareOrder",
        query: { id: record.id },
      });
    },
    // 查看出库履历页面
    handleDetail(record) {
      this.$router.push({
        path: "/picking/wareResume",
        query: {
          worksheetId: record.worksheetId,
          materialPrepareNum: record.pickListNum,
          worksheetNum: record.worksheetNum,
          worksheetQuantity: record.worksheetQuantity,
        },
      });
    },
  },
};
</script>

<style lang="less" scoped>
.card-header {
  margin: 20px 0;
}
.card-content {
  button {
    margin-right: 20px;
  }
}
::v-deep .ant-form-item {
  margin-bottom: 0;
}
</style>
